package com.naiterui.ehp.bs.cms.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.wms.entity.Product;
import com.naiterui.ehp.bs.cms.modules.wms.mapper.ProductMapper;
import com.naiterui.ehp.bs.cms.modules.wms.service.IProductService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

  @Override
  public PageVO<ProductVO> queryPage(PageParamsVO pageParamsVO, ProductVO productVO) {

    QueryWrapper<ProductVO> entityWrapper = this.buildEntityWrapper(pageParamsVO, productVO);

    IPage<ProductVO> page = this.baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);

    return PageVO.toPageVo(page);
  }

  private QueryWrapper<ProductVO> buildEntityWrapper(PageParamsVO pageParamsVO, ProductVO productVO) {
    QueryWrapper<ProductVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
    //商品名称
    String name = productVO.getName();
    entityWrapper.like(StringUtils.isNotBlank(name), "t.name", name);

    //品类
    Long type = productVO.getType();
    entityWrapper.eq(type != null, "t.type", type);

    //药理分类
    Long classificationId = productVO.getClassificationId();
    entityWrapper.eq(classificationId != null, "pc.id", classificationId);

    //是否关联sku
    Integer relationSku = productVO.getRelationSku();
    entityWrapper.eq(relationSku != null, "t.relation_sku", relationSku);

    //资料是否完整
    Integer dataIntegrity = productVO.getDataIntegrity();
    entityWrapper.eq(dataIntegrity != null, "t.data_integrity", dataIntegrity);

    entityWrapper.eq("t.valid", 1);

    return entityWrapper;
  }

}
